
/**
 *  This file is part of JSmex.
 *
 *  JSmex is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  JSmex is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  @author Tobias Senger
 *
 */

package jsmex.gui;

import java.awt.Point;
import java.awt.event.KeyEvent;
import javax.swing.JPanel;
import javax.swing.JDialog;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JLabel;

public class myCHVDialog extends JDialog {
    
    private JPanel jContentPane = null;
    private JPasswordField jPasswordField = null;
    private JButton jButton = null;
    private JButton jButton1 = null;
    private JLabel jLabel = null;
    private JLabel jLabel1 = null;
    private String chv = null;
    private final int WIDTH = 230;
    private final int HEIGTH = 150;
    
    /**
     * This is the default constructor
     */
    public myCHVDialog(javax.swing.JFrame parent, String pin) {
        super(parent, true);
        calculateLocation(parent);
        initialize();
        jLabel1.setText(pin);
        setEnabled(false);
        setEnabled(true);
        setVisible(true);
        repaint();
    }
    
    /**
     * This method initializes this
     *
     * @return void
     */
    private void initialize() {
        this.setSize(WIDTH, HEIGTH);
        this.setTitle("Enter PIN");
        this.setContentPane(getJContentPane());
    }
    
    /**
     * This method initializes jContentPane
     *
     * @return javax.swing.JPanel
     */
    private JPanel getJContentPane() {
        if (jContentPane == null) {
            jLabel1 = new JLabel();
            jLabel1.setText("");
            jLabel1.setSize(new java.awt.Dimension(60,20));
            jLabel1.setLocation(new java.awt.Point(120,10));
            jLabel = new JLabel();
            jLabel.setText("Please enter ");
            jLabel.setSize(new java.awt.Dimension(80,20));
            jLabel.setLocation(new java.awt.Point(40,10));
            jContentPane = new JPanel();
            jContentPane.setLayout(null);
            jContentPane.add(getJPasswordField(), null);
            jContentPane.add(getJButton(), null);
            jContentPane.add(getJButton1(), null);
            jContentPane.add(jLabel, null);
            jContentPane.add(jLabel1, null);
        }
        return jContentPane;
    }
    
    /**
     * This method initializes jPasswordField
     *
     * @return javax.swing.JPasswordField
     */
    private JPasswordField getJPasswordField() {
        if (jPasswordField == null) {
            jPasswordField = new JPasswordField();
            jPasswordField.setSize(new java.awt.Dimension(100,20));
            jPasswordField.setPreferredSize(new java.awt.Dimension(100,29));
            jPasswordField.setLocation(new java.awt.Point(60,45));
            jPasswordField.addKeyListener(new java.awt.event.KeyListener() {
                public void keyPressed(KeyEvent e) {
                    if (e.getKeyCode() == KeyEvent.VK_ENTER) jButton.doClick();
                }
                public void keyReleased(KeyEvent e) {
                    return;
                }
                public void keyTyped(KeyEvent e) {
                    return;
                }
            });
        }
        return jPasswordField;
    }
    
    /**
     * This method initializes jButton
     *
     * @return javax.swing.JButton
     */
    private JButton getJButton() {
        if (jButton == null) {
            jButton = new JButton();
            jButton.setSize(new java.awt.Dimension(85,20));
            jButton.setText("Ok");
            jButton.setLocation(new java.awt.Point(15,90));
            jButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    char[] chararray = jPasswordField.getPassword();
                    StringBuffer sb = new StringBuffer();
                    sb.append(chararray);
                    chv = sb.toString();
                    setVisible(false);
                    
                }
            });
        }
        return jButton;
    }
    
    /**
     * This method initializes jButton1
     *
     * @return javax.swing.JButton
     */
    private JButton getJButton1() {
        if (jButton1 == null) {
            jButton1 = new JButton();
            jButton1.setLocation(new java.awt.Point(120,90));
            jButton1.setText("Cancel");
            jButton1.setSize(new java.awt.Dimension(85,20));
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    chv=null;
                    setVisible(false);
                }
            });
        }
        return jButton1;
    }
    
    /**
     *
     * @return String which contains the PIN
     */
    public String getchv() {
        return chv;
    }
    
    private void calculateLocation(javax.swing.JFrame pf) {
        
        Point pt = pf.getLocationOnScreen();
        int fWidth = pf.getWidth();
        int fHeight = pf.getHeight();
        
        int locationx = pt.x + (fWidth - WIDTH )/2;
        int locationy = pt.y + (fHeight - HEIGTH)/2;
        setLocation(locationx, locationy);
    }
    
}  //  @jve:decl-index=0:visual-constraint="10,10"
